#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mcplugins.h"
#include "database.h"
#include "volume.h"
#include <QPluginLoader>
#include <QDebug>
#include <QtGui>

// Changes made to this file do not occur during execution on Dennis' computer for some reason.

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QApplication::setApplicationName("Maestro Media Center");
    QApplication::setWindowIcon(QIcon(":/imgs/m.png"));
    setWindowIcon(QIcon(":/imgs/m.png"));
    setWindowTitle("Maestro Media Center");
    //Querier::getResult("INSERT INTO MainSettings (settingName,value) VALUES('volume','50')");
    graphics = NULL;
    QDir currentDir = QDir(qApp->applicationDirPath());
    db=  QSqlDatabase::addDatabase("QSQLITE");
    #if defined(Q_OS_WIN)
         if (currentDir.dirName().toLower() == "debug" || currentDir.dirName().toLower() == "release")
             currentDir.cdUp();
     #elif defined(Q_OS_MAC)
         if (currentDir.dirName() == "MacOS") {
             currentDir.cdUp();
             currentDir.cdUp();
             currentDir.cdUp();
         }
     #endif

    QString database=currentDir.absolutePath()+"/databases/MediaCenter.db";
    db.setDatabaseName(database);
    /*#if defined(Q_OS_MAC)
         db.setDatabaseName("../../databases/MediaCenter.db");
     #endif
*/
    if(!db.open()) {
         QMessageBox msgBox;
         Querier::setDatabase(db);
         msgBox.setText("Database Error");
         msgBox.setInformativeText("Could not connect to database");
         msgBox.setStandardButtons(QMessageBox::Close);
         msgBox.setDefaultButton(QMessageBox::Close);
         int ret = msgBox.exec();
    } else {
        //QSqlQuery("CREATE TABLE test (test TEXT)");
        QSqlQuery query("SELECT * FROM MainSettings");

        while(query.next()) {
            qDebug() <<query.value(0).toString();
        }
        qDebug() <<"no more tables";
    }
    mainPlugins=new QList<MenuItemInterface *>();
    loadPlugins();

    //ui->gridLayout->addWidget(graphics);
    setVisible(false);
    if(graphics==NULL) {
        qDebug() <<"no graphics plugin found";
    } else {
        //qDebug() <<"graphics plugin found";
        graphics->setMainPlugins(mainPlugins);
        QWidget * widget=graphics->initialize(this);
        //Debug()<<"widget made "<<widget;
        setCentralWidget(widget);
    }
}



void MainWindow::selectPlugin(MenuItemInterface * item) {
    if(currentItem) {

    }
}

void MainWindow::loadPlugins() {
    QDir pluginsDir = QDir(qApp->applicationDirPath());

     #if defined(Q_OS_WIN)
         if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release")
             pluginsDir.cdUp();
     #elif defined(Q_OS_MAC)
         if (pluginsDir.dirName() == "MacOS") {
             pluginsDir.cdUp();
             pluginsDir.cdUp();
             pluginsDir.cdUp();
         }    
     #endif
     pluginsDir.cd("plugins");

     foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
         QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
         QObject *plugin = loader.instance();
         if (plugin) {
             MenuItemInterface * menuItem = qobject_cast<MenuItemInterface *>(plugin);
             if(menuItem) {
                 //qDebug() << menuItem->getName();
                 mainPlugins->append(menuItem);
             } else {
                 GraphicsInterface * graphicsInterface = qobject_cast<GraphicsInterface *>(plugin);
                 if(graphicsInterface) {
                    qDebug() << "graphics plugin loaded: "<<graphicsInterface;
                    this->graphics=graphicsInterface;
                } else {
                    qDebug()<<"no load";
                }
             }
         } else {
             qDebug()<<fileName <<" not a valid plugin";
             qDebug()<<loader.errorString();
         }
     }
}


void MainWindow::setWindowState(Qt::WindowStates state) {
    qDebug()<<"state changed"<<state;
    if(state.testFlag(Qt::WindowMaximized)) {
        showFullScreen();
    }
}

void MainWindow::test() {
    graphics->test();
}

MainWindow::~MainWindow()
{
    delete ui;
}

